<?php

class TestController extends Controller {

    public function testAction() {
        echo 'This is a test.';
    }

    public function tojsonAction() {
        echo json_encode($GLOBALS['config']);
    }

    public function regAction() {
        include CURRENT_VIEW_PATH . 'reg.html';
    }

    public function checkUsernameAction() {
        $username = trim($_GET['username']);
        $msg = '';
        if(empty($username)) {
            $msg = '用户名不能为空';
        }
        else {
            $testModel = Factory::M("TestModel");
            $one = $testModel->checkUsername($username);
            if($one) {
                $msg = '用户名已经存在';
            }
            else {
                $msg = '用户名可以使用';
            }
        }
        echo <<< STR
        <script type="text/javascript">
            window.parent.document.getElementById("msg").innerHTML = "$msg";
        </script>
STR;
    }

    public function doregAction() {
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);

        if(empty($username) || empty($password)) {
            $this->_jump("index.php?p=test&c=test&a=reg", "用户名或密码不能为空");
        }
        $testModel = Factory::M("TestModel");
        $one = $testModel->checkUsername($username);
        if($one) {
            $this->_jump("index.php?p=test&c=test&a=reg", "用户名已经存在");
        }
        else {
            $data = array();
            $data['admin_name'] = $username;
            $data['password'] = md5($password);
            $data['email'] = 'root@163.com';
            $data['add_time'] = date("Y-m-d H:i:s");
            if($testModel->insert($data)) {
                $this->_jump("index.php?p=back&c=admin&a=login");
            }
            else {
                $this->_jump("index.php?p=test&c=test&a=reg", "注册失败");
            }
        }
    }
}